package com.lagou.edu.interceptor;


import com.lagou.edu.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginInterceptor implements HandlerInterceptor {

	@Autowired
	private RedisUtil redisUtil;


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String requestURI = request.getRequestURI();
		System.out.println(request.getSession());
		if (requestURI.contains("login") || requestURI.contains("js")){
			return true;
		}
		String sessionName = redisUtil.get("admin");
		if(StringUtils.isEmpty(sessionName)){
			response.sendRedirect("/");
			HttpSession session = request.getSession(false);
			if(session==null){
				response.sendRedirect("/");
				return false;
			} else if(!"admin".equals(session.getAttribute("username"))){
				response.sendRedirect("/");
				return false;
			}
			return true;
		}else{
			return true;
		}

	}


}
